package com.example.smart_city_0829.activity.youth_lnn;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.activity.youth_lnn.adapter.InnExpandableListAdapter;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.YouthInn.InnList;
import com.example.smart_city_0829.entity.YouthInn.TalentPolicyAreaList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeEv;
import com.example.smart_city_0829.view.MaxSizeGv;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class YouthInnActivity extends BaseActivity {

    private ImageView im;
    private MaxSizeGv gv;
    private MaxSizeEv ev;

    private List<InnList.RowsBean> inn;
    private List<TalentPolicyAreaList.DataBean> talentPolicyList;
    private TalentGvAdapter gvAdapter;
    private InnExpandableListAdapter exAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_youth_inn;
    }

    @Override
    protected void initView() {
        im = (ImageView) findViewById(R.id.im);
        gv = (MaxSizeGv) findViewById(R.id.gv);
        ev = (MaxSizeEv) findViewById(R.id.ev);

    }

    @Override
    protected void initData() {
        api.getInnList()
                .enqueue(new NetCallBack<InnList>() {
                    @Override
                    public void onResponse(Call<InnList> call, Response<InnList> response) {
                        inn = response.body().getRows();
                        if (exAdapter == null) {
                            exAdapter = new InnExpandableListAdapter(inn, (groupPosition, isExpanded, parent) -> {
                                if (!isExpanded) {
                                    ev.expandGroup(groupPosition);
                                } else {
                                    ev.collapseGroup(groupPosition);
                                }
                            });
                            ev.setAdapter(exAdapter);
                        } else {
                            exAdapter.notifyDataSetChanged();
                        }
                    }
                });
        api.getTalentPolicyArea()
                .enqueue(new NetCallBack<TalentPolicyAreaList>() {
                    @Override
                    public void onResponse(Call<TalentPolicyAreaList> call, Response<TalentPolicyAreaList> response) {
                        Log.e("Hello Null Pointer!", response.body().getMsg());
                        talentPolicyList = response.body().getData();

                        if (gvAdapter == null) {
                            gvAdapter = new TalentGvAdapter(talentPolicyList, YouthInnActivity.this);
                            gv.setAdapter(gvAdapter);
                        } else {
                            gvAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    @Override
    protected void initEvent() {
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final TalentPolicyAreaList.DataBean dataBean = talentPolicyList.get(position);
                final Intent intent = new Intent(YouthInnActivity.this, TalentActivity.class);
                intent.putExtra("id", dataBean.getId());
                startActivity(intent);
            }
        });
        /**
         * 监听无效
         */
        ev.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                final int innId = inn.get(groupPosition).getId();
                final Intent intent = new Intent(YouthInnActivity.this, InnDetailActivity.class);
                intent.putExtra("id", innId);
                startActivity(intent);
                return true;
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "青年驿站";
    }

    private static final class TalentGvAdapter extends CommonAdapter<TalentPolicyAreaList.DataBean, TalentGvViewHolder> {

        public TalentGvAdapter(List<TalentPolicyAreaList.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(TalentPolicyAreaList.DataBean data, int position, TalentGvViewHolder viewHolder, View convertView, Context context) {
            Glide.with(convertView.getContext())
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.tv.setText(data.getName());
        }

        @Override
        protected TalentGvViewHolder getViewHolder() {
            return new TalentGvViewHolder();
        }

        @Override
        protected void initView(TalentGvViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);

        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.inn_gv_item;
        }
    }

    private final static class TalentGvViewHolder {
        ImageView im;
        TextView tv;
    }
}